package leetcode.sword;

public class Solution16 {


    public static void main(String[] args) {

        int n = Integer.MIN_VALUE;
//        System.out.println(myPow(1.0,Integer.MIN_VALUE));
        System.out.println(Integer.toBinaryString(n));
        System.out.println(Integer.toBinaryString(-n-1));
    }

    public static double myPow1(double x, int n) {
        /**
         * 快速幂
         * 把n换成2进制(m31 m30 ... m1 m0)，x^n = x^(m31*2^31+m30*2^30+...+m1*2^1+m0*2^0)
         * x^(2^m) = x^(2^(m-1))*x^(2^(m-1))
         */
        int m = Math.abs(n);
        System.out.println("mypow1:"+Integer.toBinaryString(m));
        double res = 1;
        double base = x;
        while (m!=0){
            if((m&1)==1) res *= base;
            m>>>=1; base *= base;
        }
        return n>0?res:1.0/res;
    }

    public static double myPow(double x, int n) {
        /**
         * 二分思想
         */
        //规定n>=0
        if(n==0) return 1;
        if(n<0) return 1/x*myPow(1/x, -(n+1));
        if((n&1)==1) return x*myPow(x,n-1);
        else return myPow(x*x, n>>1);
    }
}
